<%@ page contentType="text/html;charset=utf-8" %>
<%@ page import="java.util.Date" %>
<%@ page import="ut.data.User" %>
<%@ page import="ut.data.Message" %>
<%@ page import="ut.util.DateTime" %>
<%
	User user = (User)session.getAttribute("user");
	if(user == null){
		response.sendRedirect("/");
		return;
	}	
	
	int p = 1;
	try{
		if(request.getParameter("p") != null){
			p = Integer.parseInt(request.getParameter("p"));
		}
	}catch(NumberFormatException e){
		response.sendRedirect("/");
		return;
	}
		
	int maxPage = Message.getMaxMessagePage(user.id, 10);
	if(p > maxPage){
		p = maxPage;
	}
	Message[] msgs = Message.getMessages(user.id, 10, p);
%>

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>UT終極一翻！ | 我的訊息匣</title>
	<link rel="stylesheet" type="text/css" href="/style.css" media="all" />
	<link rel="stylesheet" type="text/css" href="/css/message.css" media="all" />
	<script type="text/javascript" src="/js/Utility.js"></script>
	<script type="text/javascript" src="/js/Message.js"></script>
	<script type="text/javascript" src="/js/Ajax.js"></script>
	<script type="text/javascript" src="/js/Button.js"></script>
	<script type="text/javascript" src="/js/Account.js"></script>
	<script type="text/javascript" src="/script.js"></script>
</head>
<body onload="init();">
<%@ include file="/include/banner.jsp" %>
	<div class="content">
		<div class="title">我的訊息匣</div>
		<div class="page">
<%
		StringBuilder pageOptions = new StringBuilder("");
		if(p > 1){
			pageOptions.append("<a href=\"./?page=1\">第一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p-1) + "\">上一頁</a>");
		}else{
			pageOptions.append("<span class=\"inactive\">第一頁</span><span class=\"page-link inactive\">上一頁</span>");
		}
		int startPage = (p - 2 > 0) ? p - 2 : 1;
		int endPage = startPage + 4;
		if(endPage > maxPage){
			startPage = startPage - (endPage - maxPage);
			endPage = maxPage;
		}
		for(int i = startPage; i <= endPage; i++){
			if(i < 1){
				continue;
			}else if(i == p){
				pageOptions.append("<span class=\"page-link current\">" + i + "</span>");
			}else{
				pageOptions.append("<a class=\"page-link\" href=\"./?page=" + i + "\">" + i + "</a>");
			}
		}
		if(p < maxPage){
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + (p+1) + "\">下一頁</a>");
			pageOptions.append("<a class=\"page-link\" href=\"./?page=" + maxPage + "\">最後頁</a>");
		}else{
			pageOptions.append("<span class=\"page-link inactive\">下一頁</span><span class=\"page-link inactive\">最後頁</span>");
		}
		out.println(pageOptions.toString());
%>
		</div>
<%
	if(msgs[0] == null){
%>
		<div class="task block">
			<div class="block-center">目前沒有任何訊息</div>
		</div>
<%
	}else{
		for(int i = 0; i < msgs.length; i++){
			if(msgs[i] == null || msgs[i].state == Message.DELETED){
				break;
			}
			
			String sender = User.getUserName(msgs[i].sender);
			if(msgs[i].state == Message.NEW){	//未讀訊息
%>
		<div class="task block">
			<span class="tag top blue">
				<span class="alert">NEW</span>
				寄件者：<%= sender %>
			</span>
			<div class="task-content">
				<div><%= msgs[i].content %></div>
				<div class="date">(<%= DateTime.toDateTime(msgs[i].time) %>)</div>
			</div>
			<div class="task-bottom">
<%
	/* 回信
				if(user.verify == 0){
%>
				<span class="tag bottom blue tool"
					onclick="Message.show('msg-message-create-err', 400, '權限不足', '您必須經過信箱驗證才可使用此功能', Message.BUTTON_OK">
					回信</span>
<%
				}else{
%>
				<span class="tag bottom blue tool"
					onclick="Message.show('msg-message-create', 470, '傳送訊息', Account.MessageCreate.instance);Account.MessageCreate.setValue(<%= sender.id %>, '<%= sender.name %>');Account.MessageCreate.instance.content.focus();">
					回信</span>
<%
				}
	*/
%>
				<span class="tag bottom blue tool" onclick="Account.deleteMessage(<%= msgs[i].id %>)">刪除</span>
			</div>
		</div>
<%
			}else{	//已讀訊息
%>
		<div class="task block">
			<span class="tag top green">
				寄件者：<%= sender %>
			</span>
			<div class="task-content">
				<div><%= msgs[i].content %></div>
				<div class="date">(<%= DateTime.toDateTime(msgs[i].time) %>)</div>
			</div>
			<div class="task-bottom">
<%
	/* 回信
				if(user.verify == 0){
%>
				<span class="tag bottom green tool"
					onclick="Message.show('msg-message-create-err', 400, '權限不足', '您必須經過信箱驗證才可使用此功能', Message.BUTTON_OK">
					回信</span>
<%
				}else{
%>
				<span class="tag bottom green tool"
					onclick="Message.show('msg-message-create', 470, '傳送訊息', Account.MessageCreate.instance);Account.MessageCreate.setValue(<%= sender.id %>, '<%= sender.name %>');Account.MessageCreate.instance.content.focus();">
					回信</span>
<%
				}*/
%>
				<span class="tag bottom green tool" onclick="Account.deleteMessage(<%= msgs[i].id %>)">刪除</span>
			</div>
		</div>
<%
			}
		}
	}
%>
		<div class="page">
<%
		out.println(pageOptions.toString());
		
		if(newMsgCount != 0){
			Message.checkedMessages(user.id);
		}
%>		
		</div>
<%@ include file="/include/foot.jsp" %>
	</div>
</body>
</html>